package org.wanda.pv.controller.auth;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.wanda.pv.framework.base.Result;
import org.wanda.pv.model.user.SysUser;

import java.security.Principal;

@RestController
@RequestMapping("/auth")
public class AuthController {
    @Autowired
    Dao dao;


    @RequestMapping("/success")
    public Object success(Principal principal, HttpServletRequest request){

        // 获取到登录名
        String name = principal.getName();
        // 根据登录名查询用户
        SysUser user = dao.fetch(SysUser.class, Cnd.where("loginname", "=", name));
        user.setPassword(null);
        dao.fetchLinks(user,null);
        request.getSession().setAttribute("user",user);
        return Result.success("success",user);
    }

    @RequestMapping("/fail")
    public Object fail(Principal principal){

        return Result.error("fail","用户名或密码错误!");
    }
}
